package com.lenovo.nova.util.os.application;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;

import com.lenovo.nova.util.collection.Collections;
import com.lenovo.nova.util.collection.Collections.OnShowCollectionHelper;
import com.lenovo.nova.util.debug.ObjectUtil;
import com.lenovo.nova.util.os.PackageActivityManagerUtil;
import com.lenovo.nova.util.os.application.RunningState.BaseItem;
import com.lenovo.nova.util.os.application.RunningState.MergedItem;
import com.lenovo.nova.util.os.application.RunningState.OnRefreshUiListener;
import com.lenovo.nova.util.os.application.bean.AppEntity;

public class AppManager extends StandardAppManager implements OnRefreshUiListener {

	private RunningState mRuningState;
	public AppManager(Context context) {
		super(context);
		mContext = context;
		mRuningState = RunningState.getInstance(context);
		resume();
	}
	
	public void resume(){
		mRuningState.resume(this);
	}
	
	public void waitForGetDate(){
		mRuningState.waitForData();
	}
	
	public List<AppEntity> getCurrentMergedItems(){
		waitForGetDate();
		ArrayList<MergedItem> items = mRuningState.getCurrentMergedItems();
		final List<AppEntity> result = new ArrayList<AppEntity>();
		Collections.showCollection(items, new OnShowCollectionHelper<MergedItem>(){
			@Override
			public void onItem(MergedItem e) {
				try {
					String appLable = PackageActivityManagerUtil.getCurrentInstance(mContext).getApplicatLabelName(e.mPackageInfo.packageName);
					AppEntity entity = new AppEntity(e.mPackageInfo.packageName,"",appLable);
					result.add(entity);
					new ObjectUtil(":", appLable + ":" + e.mSizeStr + ":" ,   e.mProcess.mProcessName + " :   "+ e.mPackageInfo.packageName);
				} catch (Exception e1) {
					e1.printStackTrace();
				}
			}

			@Override
			public boolean onCloseInternalPrint() {
				return false;
			}
			
		});
		return result;
	}
	
	public void showDebugInfo(){
		waitForGetDate();
		ArrayList<MergedItem> items = mRuningState.getCurrentMergedItems();
		Collections.showCollection(items, new OnShowCollectionHelper<MergedItem>(){
			@Override
			public void onItem(MergedItem e) {
				try {
					String appLable = PackageActivityManagerUtil.getCurrentInstance(mContext).getApplicatLabelName(e.mPackageInfo.packageName);
					new ObjectUtil(":", appLable + ":" + e.mSizeStr  , 
						"\tmProcessName:" + e.mProcess.mProcessName ,
						"\tpackageName:" + e.mPackageInfo.packageName);
				} catch (Exception e1) {
					e1.printStackTrace();
				}
			}

			@Override
			public boolean onCloseInternalPrint() {
				return false;
			}
			
		});
		System.out.println(ObjectUtil.formatPrint());
		
		
		System.out.println("=========progress====================");
		System.out.println();
		System.out.println();
		
		
		ArrayList<MergedItem> items1 = mRuningState.getCurrentBackgroundItems();
		ObjectUtil.clearnup();
		Collections.showCollection(items1, new OnShowCollectionHelper<MergedItem>() {
			@Override
			public void onItem(MergedItem e) {
				e.mProcess.ensureLabel(mContext.getPackageManager());
				System.out.println(e.mProcess.mDisplayLabel );
			}

			@Override
			public boolean onCloseInternalPrint() {
				return false;
			}
		});
		
		
		
		
	}

	@Override
	public void onRefreshUi(int what) {
		System.out.println("waht " + what);
	}
}
